#include"../include/lib_headers.h"

int count_nodes(node *n) {
	int c=0;
	if (n == NULL)
		return c;
	else {
		c=1+count_nodes(n->sibling)+count_nodes(n->child);
	}
}


node* find_node(node *temp, char *dest) {
	int j,match,i;
	char *s1,*p[SIZE];
	node *prev=temp;
	j = 0;
	match = 0;
	s1 = add_slash(dest);
	char *split_char = "/";
	i = splits_directory(s1, p, split_char);
	while (j < i) {
		while (temp != NULL) {
			if (strcmp(temp->fd_pointer->fileName, p[j]) == 0) {
				match = 1;
				j++;
				/* Prevents temp from becoming NULL */
				if (j == i) {
					break;
				}
				prev = temp;
				temp = temp->child;
			}
			if (match == 0) {
				prev = temp;
				temp = temp->sibling;
			} else if (match == 1) {
				match = 0;
				continue;
			}
		}
		j=i;
		/* So that if it does not match atleast it comes out of the loop. If it matches anyways ayways it will be same */
	}
	return temp;
}

node* find_prev(node *temp, char *dest) {
	int j,match,i;
	char *s1,*p[SIZE];
	node *prev=temp;
	j = 0;
	match = 0;
	s1 = add_slash(dest);
	char *split_char = "/";
	i = splits_directory(s1, p, split_char);
	while (j < i) {
		while (temp != NULL) {
			if (strcmp(temp->fd_pointer->fileName, p[j]) == 0) {
				match = 1;
				j++;
				if (j == i) {
					break;
				}
				prev = temp;
				temp = temp->child;
			}
			if (match == 0) {
				prev = temp;
				temp = temp->sibling;
			} else if (match == 1) {
				match = 0;
				continue;
			}
		}
	}
	return prev;
}


int freelist_alloc(){
	int counter=0;
	while(freelist[counter]!=0){
		counter++;
	}
	return counter;
}

char* getparent(char *s){
	char *p[SIZE]; 
	char *final=add_slash(remove_slash(remove_first_slash(s)));
	char *concat=(char*)malloc(sizeof(char)*KB);
	int i=splits_directory(final, p, "/"), j=0;
	strcpy(concat, "/");
	while(j<i-1){
		strcat(concat, p[j]);
		strcat(concat, "/");
		j++;
	}
	return concat;
	
}

void readfile_from_vfs(int blockno, fd *file){
	FILE *fp=fopen(h->VFS_Label, "rb");
	int i = 0;
	char ch;
	fseek(fp,sizeof(hd)+sizeof(fd)*h->maxFds+sizeof(int)*h->maxFds+(BLOCKSIZE)*file->blockno,SEEK_SET);

	/* rb+ means to overwrite */
	FILE *f = fopen(h->VFS_Label, "rb+");
	fseek(f,sizeof(hd)+sizeof(fd)*h->maxFds+sizeof(int)*h->maxFds+blockno*(BLOCKSIZE),SEEK_SET);

	while(i != file->fileSize){
		ch = getc(fp);
		putc(ch, f);
		i++;
	}

	fclose(fp);
	fclose(f);
}

int write_into_file(int blockno, char *data_file_path){
	FILE *fp=fopen(h->VFS_Label, "rb+");
	fseek(fp,sizeof(hd)+sizeof(fd)*h->maxFds+sizeof(int)*h->maxFds+blockno*(BLOCKSIZE),SEEK_SET);
	int i = 0;char ch;
	
	FILE *f = fopen(data_file_path,"rb");	
	fseek(f,0,SEEK_END);
	long int x=ftell(f);
	rewind(f);
	while(i != x){
		ch = getc(f);
		putc(ch, fp);
		i++;
	}
	fclose(fp);
	return 1;
}

char* readfile(char *data_file_path){
	
	FILE *fp = fopen(data_file_path,"rb");
	fseek(fp,0,SEEK_END);
	long int x=ftell(fp);
	char *str=(char*)malloc(sizeof(char)*(BLOCKSIZE));
	rewind(fp);
	fread(str,x,1,fp);
	return str;
}

long int length_of_file(FILE *fp){
	fseek(fp, 0, SEEK_END);
	return ftell(fp); 
}

